/**
 * zoneland.net Inc.
 * Copyright (c) 2002-2013 All Rights Reserved.
 */
package com.foodoon.monitor.web.home.admin;

import java.security.MessageDigest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foodoon.monitor.web.home.BaseController;
import com.foodoon.monitor.web.home.vo.Response;

/**
 * 
 * @author foodoon
 * @version $Id: KeyController.java, v 0.1 2013-6-19 上午7:44:57 foodoon Exp $
 */
@Controller
public class KeyController extends BaseController {

    @RequestMapping(value = "/key.htm", method = RequestMethod.GET)
    public String doGet(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        return "key.vm";
    }

    @RequestMapping(value = "createKey.json")
    public void createKey(HttpServletRequest request, HttpServletResponse response,
                          ModelMap modelMap) {

        String k = (request.getParameter("key"));

        Response res = new Response();

        res.setSuccess(true);
        res.setMsg(md5(k));

        super.ajaxReturn(res, response);

    }

    public final static String md5(String plainText) {
        if (plainText == null) {
            return null;
        }
        plainText = plainText + "medi";
        String md5Str = null;
        try {
            StringBuffer buf = new StringBuffer();
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();
            int i;
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));
            }
            md5Str = buf.toString();
        } catch (Exception e) {

        }
        return md5Str;
    }

}
